package mb.admin.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.xml.xpath.XPathExpressionException;

import mb.admin.data.NewsData;
import mb.commons.AbstractDAO;
import mb.commons.DbConnection;
import mb.commons.XmlHelper;

/**
 * Klasa Managera obsługującego widok News'ów
 * @author Michał Budziło
 *
 */
public class NewsDAO extends AbstractDAO{
	
	/**
	 * Konstruktor
	 */
	public NewsDAO() {
		conn = DbConnection.getConnection();
		helper = new XmlHelper("WEB-INF/xml/admin/news.xml");
	}

	/**
	 * Metoda zwraca z bazy danych listę news'ów
	 * @return lista użytkowników
	 */
	public ArrayList<NewsData> getNewsList() {
		ArrayList<NewsData> list = new ArrayList<NewsData>();
		NewsData data;
		try {
			query = helper.getSqlString("newsList","sqlString/prepareMain/sql");
			stmt = conn.createStatement();
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				data = new NewsData();
				
				data.setId(rs.getInt("id"));
				data.setDodalImie(rs.getString("imie"));
				data.setDodalNazwisko(rs.getString("nazwisko"));
				data.setTresc(rs.getString("tresc"));
				data.setDataDodania(rs.getDate("data_dodania"));
				data.setJezyk(rs.getInt("jezyk"));
				list.add(data);
			}
			return list;
		} catch (SQLException e) {
			error(e.getErrorCode() + " : " + e.getMessage());
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}
		return null;
	}
	
	
	/**
	 * Metoda wstawia do bazy danych nowego news'a
	 * @param data obiekt klasy news
	 */
	public void insertData(NewsData data) {
		try {
			query = helper.getSqlString("insertData","sqlString/prepareMain/prepareStatment");
			prepStmt = conn.prepareStatement(query);
		
			prepStmt.executeUpdate();
			
		} catch (SQLException e) {
			error(e.getErrorCode() + " : " + e.getMessage());
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			
			close();
		}
		
	}
	
	 /**
	  * Metoda aktualizuje w bazie danych wybranego news'a
	 * @param data obiekt klasy news
	 */
	public void updateData(NewsData data) {
		try {
			query = helper.getSqlString("updateData","sqlString/prepareMain/prepareStatment");
			prepStmt = conn.prepareStatement(query);
			prepStmt.setString(1, data.getTresc());
			prepStmt.setInt(2, data.getId());
			
			
			prepStmt.executeUpdate();
			
		} catch (SQLException e) {
			error(e.getErrorCode() + " : " + e.getMessage());
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			
			close();
		}
		
	}
	 
	 /**
	  * Metoda usuwa z bazy danych wybranego news'a
	 * @param data obiekt klasy news
	 */
	public void deleteData(NewsData data) {
			
			try {
				query = helper.getSqlString("deleteData","sqlString/prepareMain/prepareStatment");
				prepStmt = conn.prepareStatement(query);
				prepStmt.setInt(1, data.getId());
				
				prepStmt.executeUpdate();
				
			} catch (SQLException e) {
				error(e.getErrorCode() + " : " + e.getMessage());
				e.printStackTrace();
			} catch (XPathExpressionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				
				close();
			}
		}
	 

}
